﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Activities;
using Vowei.Core.Models;
using Vowei.Core.Scm;

namespace Vowei.Workflows.Tickets
{
    public sealed class CreateReviewTasksActivity : CodeActivity
    {
        public InArgument<Ticket> Ticket { get; set; }

        public InArgument<IVoweiContext> DatabaseContext { get; set; }

        public InArgument<IReviewPolicy> ReviewPolicy { get; set; }

        public OutArgument<Task[]> ReviewTasks { get; set; }

        protected override void Execute(CodeActivityContext context)
        {
            var ticket = Ticket.Get(context);
            var dbcontext = DatabaseContext.Get(context);
            var policy = ReviewPolicy.Get(context);
            var tasks = new List<Task>();

            if ( policy.ReviewNeeded(ticket) )
                tasks.AddRange(policy.CreateReviewTasksFor(ticket, dbcontext));

            ReviewTasks.Set(context, tasks.ToArray());
        }
    }
}
